package com.aijk.ehm.api;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.ehm.model.EhmClassifySubscribe;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.qo.EhmClassifySubscribeQo;
import com.aijk.ehm.service.EhmClassifySubscribeService;
import com.aijk.ehm.service.EhmInfoClassifyService;
import com.aijk.ehm.service.EhmResidentService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 标签订阅管理
 */
@Service("ehmClassifySubscribeApiImpl")
@Transactional
public class EhmClassifySubscribeApiImpl implements EhmClassifySubscribeApi {
    private static Logger logger = LoggerFactory.getLogger(EhmClassifySubscribeApiImpl.class);

    @Autowired
    private EhmClassifySubscribeService service;

    @Autowired
    private EhmResidentService ehmResidentService;

    @Autowired
    private EhmInfoClassifyService ehmInfoClassifyService;

    @Override
    public Long save(EhmClassifySubscribe bean) throws ErrorCodeException {
        service.save(bean);
        return bean.getId();
    }

    @Override
    public void update(EhmClassifySubscribe bean) throws ErrorCodeException {
        service.update(bean);
    }

    @Override
    public EhmClassifySubscribe findOne(Long id) throws ErrorCodeException {
        return service.findOne(id);
    }

    @Override
    public List<EhmClassifySubscribe> list(EhmClassifySubscribeQo qo) {
        return service.find(qo);
    }

    @Override
    public void deleteById(Long id) throws ErrorCodeException {
        service.delete(id);
    }

    @Override
    public void delete(EhmClassifySubscribeQo qo) throws ErrorCodeException {
        service.delete(qo);
    }

    @Override
    public int count(EhmClassifySubscribeQo qo) {
        return service.count(qo);
    }

    @Override
    public PageInfo<EhmClassifySubscribe> page(EhmClassifySubscribeQo qo) {
        PagingRequest pagingRequest = new PagingRequest(qo.getPageNo().intValue(), qo.getPageSize().intValue());
        return service.page(pagingRequest,qo);
    }

    @Override
    public void saves(List<Long> ids,Long id){
        EhmClassifySubscribeQo ehmClassifySubscribeQo = new EhmClassifySubscribeQo();
        ehmClassifySubscribeQo.setCreater(id);
        service.delete(ehmClassifySubscribeQo);
        EhmResident ehmResident = ehmResidentService.findOne(id);
        for(Long cid : ids){
            EhmClassifySubscribe ehmClassifySubscribe1 = new EhmClassifySubscribe();
            ehmClassifySubscribe1.setCid(cid);
            ehmClassifySubscribe1.setType(0);
            ehmClassifySubscribe1.setCreaterUser(ehmResident.getTitle());
            ehmClassifySubscribe1.setCreater(id);
            ehmClassifySubscribe1.setCreated(new Date());
            service.save(ehmClassifySubscribe1);
        }

    }
}